package com.sgb.kjwl.view.ui.adapter.self;

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.businessmanagement.CompanyEntity;

import java.util.List;
/*
主企业列表适配器
 */
public class MainEnterprisesListAdapter extends RecyclerView.Adapter<MainEnterprisesListAdapter.MyVh> {
    Context context;
    List<CompanyEntity> list;
    //主企业列表适配器的有参方法用于传参
    public MainEnterprisesListAdapter(Context context, List<CompanyEntity> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyVh onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        //设置视图
        View inflate = View.inflate(context, R.layout.adapter_mainenterpriseslistadapter_item, null);
        MyVh myVh = new MyVh(inflate);
        return myVh;
    }

    @Override
    public void onBindViewHolder(@NonNull MyVh myVh, @SuppressLint("RecyclerView") int i) {
        //给视图里的控件赋值
      myVh.corporate_name.setText(list.get(i).getCompName());
        //给视图里的控件设置点击监听
        myVh.switching_mainEnterprises.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemClickListener.onItemClick(i);
            }
        });
    }

    @Override
    public int getItemCount() {
        // 设置企业列表的数量
        return list.size();
    }
    public class MyVh extends  RecyclerView.ViewHolder {

        private final TextView corporate_name;
        private final TextView switching_mainEnterprises;

        public MyVh(@NonNull View itemView) {
            super(itemView);
            //找到视图的控件
            corporate_name = itemView.findViewById(R.id.corporate_name);
            switching_mainEnterprises = itemView.findViewById(R.id.tv_Switching_mainEnterprises);
        }
    }
    //创建一个接口
    public interface OnItemClickListener{
        void onItemClick(int position);
    }
    private OnItemClickListener mItemClickListener;
    //暴露一个方法方便给别的类去调用
    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }
}